# coding=utf-8
from django import template


#from django.http import QueryDict
#QueryDict
from django.contrib.auth.models import User

__author__ = 'czq'

register = template.Library()

@register.inclusion_tag('tags/dummy.html')
def theme_rank(request,rank, template="BbsTheme/tags/theme_rank.html"):
    from DjangoBbs.Common.Final import rankList
    return {
            "template": template,
            'rank':rank,
            'rankList':rankList,
            }

@register.inclusion_tag('tags/dummy.html')
def theme_filter(request,filter, template="BbsTheme/tags/theme_filter.html"):
    from DjangoBbs.Common.Final import filterList
    return {
            "template": template,
            'filter':filter,
            'filterList':filterList,
            }

@register.inclusion_tag('tags/dummy.html')
def theme_filter_rank(request,template="BbsTheme/tags/theme_filter_rank.html"):
    order_type=request.GET.get('order_type','1')

    queryList=request.GET.copy()
    try:
        queryList.pop('order_type')
    except:
        pass
    from DjangoBbs.Common.Final import filterList,rankList
    return {
            "template": template,
            #'filter':filter,
            #'rank':rank,
            'filterList':filterList,
            'rankList':rankList,
            'order_type':order_type,
            #'uid':uid,
            #'value':value,
            'request':request,
            'queryList':queryList.iteritems(),
            #'queryStr':queryList.urlencode(),
            }

#@register.inclusion_tag('tags/dummy.html')
def theme_get_list(request,classId=None,isSearch=False,isUser=False,template="BbsTheme/tags/theme_list.html"):
    if classId==None:
        bbsClass=None
    else:
        from DjangoBbs.BbsTheme.models import BbsClass
        bbsClass=BbsClass.objects.get(id=classId)
    if True:
        from DjangoBbs.Common.Theme_Filter import Theme_Filter_Rank
        list = Theme_Filter_Rank(request,bbsClass,isSearch=isSearch,isUser=isUser)
        #return list
        return {
            "template": template,
            'list':list,
            'request':request,
            'isSearch':isSearch,
            'isUser':isUser,
            'classId':classId,
            'bbsClass':bbsClass,
            'url':'RideLine_show',
            #'isSearch':isSearch,
            }
@register.inclusion_tag('tags/dummy.html')
def theme_list_class(request,classId=None,template="BbsTheme/tags/theme_list.html"):
    return theme_get_list(request,classId=classId,template=template)

@register.inclusion_tag('tags/dummy.html')
def theme_list_user(request,classId=None,template="BbsTheme/tags/theme_list.html"):
    return theme_get_list(request,classId=classId,template=template,isSearch=True,isUser=True)

@register.inclusion_tag('tags/dummy.html')
def theme_list_search(request,classId=None,template="BbsTheme/tags/theme_list.html"):
    classId=request.GET.get('bbsClass','')
    if classId=='':
        classId=None
    return theme_get_list(request,classId=classId,template=template,isSearch=True)

def theme_reply_get_list(request,theme=None,isSearch=False,isUser=False,template="BbsTheme/tags/theme_reply_list.html"):
    from DjangoBbs.BbsTheme import models
    list=models.BbsReply.objects_show.filter(classId__visitPerm=None)
    if isUser:
        uid=request.GET.get('uid',None)
        if uid!=None:
            user=User.objects.get(id=uid)
            list=list.filter(addUser=user)
    if theme==None:
        pass
    else:
        list=list.filter(theme=theme)
    if True:
        #from DjangoBbs.Common.Theme_Filter import Theme_Filter_Rank
        #list = Theme_Filter_Rank(request,theme,isSearch=isSearch,isUser=isUser)
        #return list
        return {
            "template": template,
            'list':list,
            'request':request,
            'isSearch':isSearch,
            'isUser':isUser,
            #'isSearch':isSearch,
            }

@register.inclusion_tag('tags/dummy.html')
def theme_reply_list_theme(request,theme=None,template="BbsTheme/tags/theme_reply_list.html"):
    return theme_reply_get_list(request,theme=theme,template=template)

@register.inclusion_tag('tags/dummy.html')
def theme_reply_list_user(request,theme=None,template="BbsTheme/tags/theme_reply_list.html"):
    return theme_reply_get_list(request,theme=theme,template=template,isUser=True)